package com.aysafety.iotdataplatformweb.controller.log;

import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.aysafety.core.common.Common;
import com.aysafety.core.common.DateUtils;
import com.aysafety.iotdataplatformweb.controller.base.BaseController;
import com.aysafety.iotdataplatformweb.services.log.LogService;

@RestController
@RequestMapping("/api/iotLog/")
public class LogController extends BaseController {

    private Logger logger = LoggerFactory.getLogger(this.getClass());

    @Autowired
    LogService logService;

    @RequestMapping("/getIotErrorLog")
    public Map<String, Object> getIotErrorLog(@RequestParam Map<String, Object> params, HttpServletRequest request,
        HttpServletResponse response) {
        try {
            Map<String, Object> result = new HashMap<String, Object>();
            Map<String, Object> data = new HashMap<String, Object>();
            if (!params.containsKey("clientId")) {
                return this.formatReturnData("缺少参数");
            }
            String clientId = params.get("clientId").toString();
            String startTime = params.containsKey("startTime") ? params.get("startTime").toString()
                : DateUtils.getCurrentStartTime("yyyy-MM-dd HH:mm:ss");
            startTime =
                StringUtils.isNotBlank(startTime) ? startTime : DateUtils.getCurrentStartTime("yyyy-MM-dd HH:mm:ss");
            String endTime = params.containsKey("endTime") ? params.get("endTime").toString()
                : DateUtils.getCurrentTime("yyyy-MM-dd HH:mm:ss");
            endTime = StringUtils.isNotBlank(endTime) ? endTime : DateUtils.getCurrentTime("yyyy-MM-dd HH:mm:ss");
            String pageStr = params.containsKey("page") ? params.get("page").toString() : "0";
            pageStr = StringUtils.isNotBlank(pageStr) ? pageStr : "0";
            int page = Integer.parseInt(pageStr);
            String sizeStr = params.containsKey("size") ? params.get("size").toString() : "100";
            sizeStr = StringUtils.isNotBlank(sizeStr) ? sizeStr : "100";
            int size = Integer.parseInt(sizeStr);
            String startRow = params.containsKey("startRow") ? params.get("startRow").toString() : "";
            String firstStartRow = params.containsKey("firstStartRow") ? params.get("firstStartRow").toString() : "";
            data.put("total", logService.countErrorLog(clientId, startTime, endTime));
            result = Common.getData(logService.queryErrorLog(clientId, startTime, endTime, page, size, startRow), size,
                startRow, firstStartRow);
            data.put("list", result.get("list"));
            data.put("firstStartRow", result.get("firstStartRow"));
            data.put("lastStartRow", result.get("lastStartRow"));
            return this.formatReturnData(data);
        } catch (Exception e) {
            logger.error("LogController-getIotErrorLog:", e);
            return this.formatReturnData(e.getMessage());
        } catch (Throwable throwable) {
            logger.error("LogController-getIotErrorLog:", throwable);
            return this.formatReturnData(throwable.getMessage());
        }
    }
}
